package com.speedata;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.SystemProperties;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.king.zxing.util.CodeUtils;
import com.speedata.uhftest.R;


/**
 * @author xuyan  读电话号码并显示,用原H6的读取方式读对应参数
 */
public class PhoneActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private Button bt6;
    private Button bt7;
    private Button bt8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);
        initView();
    }

    private void initView() {
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt3 = findViewById(R.id.bt3);
        bt4 = findViewById(R.id.bt4);
        bt5 = findViewById(R.id.bt5);
        bt6 = findViewById(R.id.bt6);
        bt7 = findViewById(R.id.bt7);
        bt8 = findViewById(R.id.bt8);

        bt1.setOnClickListener(this);
        ivCode = findViewById(R.id.ivCode);

    }


    @SuppressLint({"HardwareIds", "NonConstantResourceId"})
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt1) {
            TelephonyManager phoneMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            //显示手机号
            bt1.setText(phoneMgr.getLine1Number());
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onResume() {
        super.onResume();

        bt3.setText("SN:" + SystemProperties.get("ro.serialno"));
        bt4.setText("VERSION:" + SystemProperties.get("ro.build.display.id"));
        bt5.setText("ICCID:" + SystemProperties.get("ril.iccid.sim1"));
        bt6.setText("IMEI:" + SystemProperties.get("persist.sys.imei1"));
        bt7.setText("IMSI:" + SystemProperties.get("ril.data.gsm_imsi1"));
        bt8.setText("ESIM:" + SystemProperties.get("ril.data.get_esim"));

        createBarCode(SystemProperties.get("ril.iccid.sim1", "NOICCID"));
        //createBarCode("898989B9898989898989");
    }


    private ImageView ivCode;


    /**
     * 生成条形码
     *
     * @param content 条码
     */
    private void createBarCode(String content) {
        new Thread(() -> {
            //生成条形码相关放在子线程里面
            Bitmap bitmap = CodeUtils.createBarCode(content, BarcodeFormat.CODE_128, 800, 200, null, true);
            runOnUiThread(() -> {
                //显示条形码
                ivCode.setImageBitmap(bitmap);
            });
        }).start();
    }

}
